package com.cw.jeeyt.controller.system;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.utils.ConvertSwf;
import com.cw.jeeyt.common.utils.FileOperatorUtil;
import com.cw.jeeyt.common.utils.WordGenerator;

@Controller
@RequestMapping("wordFactory")
public class WordController {
    private String templatePath = "";
    private String outputFile = "";

    @RequestMapping("/createWord")
    @ResponseBody
    public JsonResultDto createPdfFile(HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            String root = Thread.currentThread()
                .getContextClassLoader()
                .getResource("../../")
                .getPath()
                .substring(1) + "resources" + File.separator + "itext";
            templatePath = root + File.separator + "templates" + File.separator;
            outputFile = root + File.separator + "word";
            File outputFileDirectory = new File(outputFile);
            if (!outputFileDirectory.exists()) {
                outputFileDirectory.mkdir();
            }
            Map<String, Object> variables = new HashMap<>();
            variables.put("title", "借款合同");
            variables.put("applyUser", "chenw");
            //HtmlGenerator.generate(templatePath, "cover.ftl", variables);
            WordGenerator.createWord(variables, "cover.ftl", templatePath, outputFile, "cover.doc");
            //将word转成swf
            ConvertSwf.convertPDF("cover.doc", outputFile, "");
            jsonResultDto.setSuccess(true);
            Map<String, Object> map = new HashMap<>();
            map.put("wordPath", outputFile);
            jsonResultDto.setResultData(map);
        } catch (Exception e) {

            jsonResultDto.setMsg("pdf生成出错：" + e.getMessage());
            e.printStackTrace();
        }
        return jsonResultDto;
    }

    /**
     * 在线预览pdf
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/viewWord")
    public ModelAndView viewPdfFile() {
        ModelAndView mv = new ModelAndView("itext/viewword/word_form");
        mv.addObject("fileName", "cover1");
        return mv;
    }

    /**
     * word文件下载
     *
     * @return
     * @author chenw;
     * @version v1.0 chenw 2014-7-28 创建方法
     * @since 2014-7-28 上午11:42:04
     */
    @RequestMapping("/downloadWord")
    public void DownloadPDF(HttpServletRequest request, HttpServletResponse response) {

        String root = Thread.currentThread()
            .getContextClassLoader()
            .getResource("../../")
            .getPath()
            .substring(1) + "resources" + File.separator + "itext";
        outputFile = root + File.separator + "word";
        try {
            outputFile = outputFile + File.separator + "cover.doc";
            File file = new File(outputFile);
            if (file.exists()) {
                FileOperatorUtil.download(request, response, outputFile, "application/msword", "cover.doc");
            }
        } catch (Exception e) {
            System.err.println("下载内容附件出错：" + e.getMessage());
        }
    }

}
